class IUP_LIST_DIALOG
-- Shows a modal dialog to select items from a simple or multiple selection 
-- list.
--
-- The dialog uses a global attribute called "PARENTDIALOG" as the parent 
-- dialog if it is defined. It also uses a global attribute called "ICON" as 
-- the dialog icon if it is defined.

inherit
	IUP_WIDGET

create {ANY}
   list_dialog_simple,
   list_dialog_multiple

feature {ANY}

	list_dialog_simple (title: STRING; list: ARRAY[STRING];
                       op, max_col, max_lin: INTEGER)
		-- Create a dialog for simple selection.
		--
		-- title: Text for the dialog’s title.
		-- list: List of options.
		-- op: Initial selected item. Starts at 1 (note that this index is 
		-- different from the return value, kept for compatibility reasons).
		-- max_col: number of visible columns in the list.
		-- max_lin: number of visible lines in the list.
		do
			t := 1
			ttl := title
			l := list
			o := op
			mc := max_col
			ml := max_lin

			convert_to_native(list)
		end

	list_dialog_multiple (title: STRING; list: ARRAY[STRING];
                         max_col, max_lin: INTEGER)
		-- Create a dialog for multiple selection.
		--
		-- title: Text for the dialog’s title.
		-- list: List of options.
		-- max_col: number of visible columns in the list.
		-- max_lin: number of visible lines in the list.
		do
			t := 2
			ttl := title
			l := list
			o := 0
			mc := max_col
			ml := max_lin

			convert_to_native(list)
		end

	launch: TUPLE[INTEGER, STRING]
			-- Show the dialog. If is a dialog for simple selection, the tuple 
			-- return an integer with the index of the selected item (starts 
	      -- at 0), or -1 if the user cancels the operation. Also return the 
			-- item itself, or an empty string if the user cancel the operation.
			-- If is a dialog of multiple selection, the function returns -1 when 
			-- the user cancels the operation and an empty string. If the user 
			-- does not cancel the operation the function returns 1 and the string 
			-- parameter will have value 1 for the options selected by the user 
			-- and value 0 for non-selected options (for example: "0010011100").
		local
			i: INTEGER
			so: STRING
			marks: ARRAY[INTEGER]
		do
			create marks.make_filled(0, 1, l.count)

			
			i := int_list_dialog (t, get_pointer(ttl.to_c),
			                      l.count, get_pointer(arg.to_c), o, mc, ml,
			                      get_pointer(marks.to_c))

			if i.is_equal(-1) then
				Result := [i, ""]
			else
				if t.is_equal(1) then
					Result := [i, l.item(i)]
				else
					so := ""

					across
					   marks as ic
					loop
						so.append_string(ic.item.out)
					end

					Result := [i, so]
				end
			end
		end

feature {NONE}

	t, o, mc, ml: INTEGER
	ttl: STRING
	l: ARRAY[STRING]

	arg: ARRAY[POINTER]

	-- Internals
	
	int_list_dialog (type: INTEGER; title: POINTER; size: INTEGER; list: POINTER; op, max_col, max_lin: INTEGER; marks: POINTER): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupListDialog ($type, $title, $size, $list, $op, $max_col, $max_lin, $marks);"
      end

	convert_to_native (col: ARRAY[STRING])
		local
			i: INTEGER; s: STRING
		do
			i := col.count
			create arg.make_filled(default_pointer, 1, i + 1)

			i := 0
			
			across
			   col as ic
	      loop
		      i := i + 1
            s := ic.item
            arg.put(get_pointer(s.to_c), i)
         end
		end

end -- class IUP_LIST_DIALOG

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
